package com.bsi.limiter.keyresolver;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * @ClassName KeyResolverConfiguration
 * @Author LiuSong
 * @Date 2020/7/31 16:34
 * @Version 1.0
 * @Description 令牌桶的配置
 */
@Configuration
public class KeyResolverConfiguration {
    /**
     * 基于请求路径的限流
     */
    @Bean
    public KeyResolver pathKeyResolver() {
        return exchange -> Mono.just(
                exchange.getRequest().getPath().toString()
        );
    }
//
//    /**
//     * 基于ip的限流
//     */
//    @Bean
//    public KeyResolver ipKeyResolver() {
//        return exchange -> Mono.just(
//                exchange.getRequest().getHeaders().getFirst("X-Forwarded-For")
//        );
//    }
//
//    /**
//     * 基于用户的限流
//     */
//    @Bean
//    public KeyResolver userKeyResolver() {
//        return exchange -> Mono.just(
//                exchange.getRequest().getHeaders().getFirst("Authorization")
//        );
//    }
}
